package com.qiguliuxing.dts.wx.service;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

import com.qiguliuxing.dts.core.util.CharUtil;
import com.qiguliuxing.dts.core.util.RedisKeyUtils;
import com.qiguliuxing.dts.wx.dao.UserToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 维护用户token
 */
@Component
public class UserTokenManager {
	@Autowired
	private RedisTemplate redisTemplate;

	private static RedisTemplate staticRedisTemplate;

//	private static Map<String, UserToken> tokenMap = new HashMap<>();
//	private static Map<Integer, UserToken> idMap = new HashMap<>();

	@PostConstruct
	public void init(){
		staticRedisTemplate = redisTemplate;
	}

	public static Integer getUserId(String token) {

		//UserToken userToken = tokenMap.get(token);
		UserToken userToken = (UserToken) staticRedisTemplate.opsForValue().get(RedisKeyUtils.getTokenMapKey(token));
		if (userToken == null) {
			return null;
		}

//		if (userToken.getExpireTime().isBefore(LocalDateTime.now())) {
//			tokenMap.remove(token);
//			idMap.remove(userToken.getUserId());
//			return null;
//		}

		return userToken.getUserId();
	}

	public static UserToken generateToken(Integer id) {
		UserToken userToken = null;

		// userToken = idMap.get(id);
		// if(userToken != null) {
		// tokenMap.remove(userToken.getToken());
		// idMap.remove(id);
		// }

		String token = CharUtil.getRandomString(32);
		String tokenMapKey = RedisKeyUtils.getTokenMapKey(token);
		//while (tokenMap.containsKey(token)) {
		while (staticRedisTemplate.hasKey(tokenMapKey)) {
			token = CharUtil.getRandomString(32);
			tokenMapKey = RedisKeyUtils.getTokenMapKey(token);
		}

		LocalDateTime update = LocalDateTime.now();
		LocalDateTime expire = update.plusDays(1);

		userToken = new UserToken();
		userToken.setToken(token);
		userToken.setUpdateTime(update);
		userToken.setExpireTime(expire);
		userToken.setUserId(id);
		//tokenMap.put(token, userToken);
		//idMap.put(id, userToken);
		staticRedisTemplate.opsForValue().set(tokenMapKey, userToken, 1, TimeUnit.DAYS);
		staticRedisTemplate.opsForValue().set(RedisKeyUtils.getIdMapKey(id), userToken, 1, TimeUnit.DAYS);

		return userToken;
	}

	public static String getSessionKey(Integer userId) {
		//UserToken userToken = idMap.get(userId);
		UserToken userToken = (UserToken) staticRedisTemplate.opsForValue().get(RedisKeyUtils.getIdMapKey(userId));
		if (userToken == null) {
			return null;
		}

//		if (userToken.getExpireTime().isBefore(LocalDateTime.now())) {
//			tokenMap.remove(userToken.getToken());
//			idMap.remove(userId);
//			return null;
//		}

		return userToken.getSessionKey();
	}

	public static void removeToken(Integer userId) {
		//UserToken userToken = idMap.get(userId);
		UserToken userToken = (UserToken) staticRedisTemplate.opsForValue().get(RedisKeyUtils.getIdMapKey(userId));
		if (userToken != null){
			String token = userToken.getToken();
			staticRedisTemplate.delete(RedisKeyUtils.getIdMapKey(userId));
			staticRedisTemplate.delete(RedisKeyUtils.getTokenMapKey(token));
		}
		//String token = userToken.getToken();
		//idMap.remove(userId);
		//tokenMap.remove(token);
	}
}
